/**
 * @file engineer_mode_keep_screen_on.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_ENGINEER_MODE != 0

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void keep_screen_on_btn_action(lv_obj_t * cb, lv_event_t event);
static void keep_screen_off_btn_action(lv_obj_t * cb, lv_event_t event);

/**********************
 *  STATIC VARIABLES
 **********************/
static bool is_lcd_on = false;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * engineer_mode_keep_screen_on_create(lv_obj_t * activity_obj)
{
    lv_obj_t * obj;
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_ENGINEER_MODE_KEEP_SCREEN_ON;
        activity_ext.create = engineer_mode_keep_screen_on_create;
        activity_ext.prepare_destory = NULL;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    lv_obj_t * title = lv_label_create(obj, NULL);
    lv_label_set_align(title, LV_LABEL_ALIGN_CENTER);
    lv_label_set_long_mode(title, LV_LABEL_LONG_SROLL);
    lv_obj_set_size(title, LV_HOR_RES, LV_VER_RES / 4);
    lv_label_set_text_id(title, WATCH_TEXT_ID_ENGINEER_MODE_KEEP_SCREEN_ON);
    lv_obj_add_style(title, LV_LABEL_PART_MAIN, &lv_watch_font30);
    lv_obj_align(title, NULL, LV_ALIGN_IN_TOP_MID, 0, 5);

    lv_obj_t * cont = lv_cont_create(obj, NULL);
    lv_obj_add_style(cont, LV_OBJ_PART_MAIN, &lv_style_transp_fit);
    if(lv_obj_get_base_dir(obj) == LV_BIDI_DIR_RTL)
        lv_btn_set_layout(cont, LV_LAYOUT_COLUMN_RIGHT);
    else
        lv_btn_set_layout(cont, LV_LAYOUT_COLUMN_LEFT);
    lv_obj_align(cont, NULL, LV_ALIGN_CENTER, -10, -20);
    lv_cont_set_fit2(cont, LV_FIT_TIGHT, LV_FIT_TIGHT);
    lv_obj_set_click(cont, false);

    lv_obj_t * cb_on = lv_checkbox_create(cont, NULL);
    lv_checkbox_set_text(cb_on, lv_lang_get_text(WATCH_TEXT_ID_ON));
    lv_obj_clean_style_list(cb_on, LV_CHECKBOX_PART_BULLET);
    lv_obj_add_style(cb_on, LV_CHECKBOX_PART_BULLET, &lv_watch_cb_styles);
    lv_obj_set_event_cb(cb_on, keep_screen_on_btn_action);
    lv_watch_obj_add_element(cb_on);

    lv_obj_t * cb_off = lv_checkbox_create(cont, cb_on);
    lv_checkbox_set_text(cb_off, lv_lang_get_text(WATCH_TEXT_ID_OFF));
    lv_obj_set_event_cb(cb_off, keep_screen_off_btn_action);
    lv_watch_obj_add_element(cb_off);

    if(is_lcd_on) {
        lv_checkbox_set_checked(cb_on, true);
    } else {
       lv_checkbox_set_checked(cb_off, true);
    }

    return obj;
}

bool keep_screen_on_get_status(void)
{
    printf("%s: is_lcd_on %d\n", __FUNCTION__, is_lcd_on);
    return is_lcd_on;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void keep_screen_on_btn_action(lv_obj_t * cb, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    printf("%s: is_lcd_on %d\n", __FUNCTION__, is_lcd_on);

    lv_checkbox_set_checked(cb, true);

    if(false == is_lcd_on) {
        is_lcd_on = true;
        lv_obj_t * cont = lv_obj_get_parent(cb);
        lv_checkbox_set_checked(lv_obj_get_child_back(cont, cb), false);
    }
}

static void keep_screen_off_btn_action(lv_obj_t * cb, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    printf("%s: is_lcd_on %d\n", __FUNCTION__, is_lcd_on);

    lv_checkbox_set_checked(cb, true);

    if(is_lcd_on) {
        is_lcd_on = false;
        lv_obj_t * cont = lv_obj_get_parent(cb);
        lv_checkbox_set_checked(lv_obj_get_child(cont, cb), false);
    }
}

#endif /*USE_LV_WATCH_ENGINEER_MODE*/
